package com.web.graduationpro.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.web.graduationpro.dao.service.ArticleService;
import com.web.graduationpro.dao.service.GoodsService;
import com.web.graduationpro.dao.service.InstrumentSchoolService;
import com.web.graduationpro.dao.service.MemberService;
import com.web.graduationpro.dao.service.ShoppingCartMiddleService;
import com.web.graduationpro.dao.service.ShoppingCartService;
import com.web.graduationpro.dao.service.TeacherService;
import com.web.graduationpro.model.Manager;
import com.web.graduationpro.model.Member;
import com.web.graduationpro.model.ShoppingCart;
import com.web.graduationpro.model.ShoppingCartMiddle;
import com.web.graduationpro.util.MD5;

@Controller
@RequestMapping("/usercenter/*")
public class UserCenterController {

	@Resource(name="memberServiceImpl")
	private MemberService memberServiceImpl;
	
	@Resource(name="instrumentSchoolServiceImpl")
	private InstrumentSchoolService instrumentSchoolServiceImpl;
	
	@Resource(name="teacherServiceImpl")
	private TeacherService teacherServiceImpl;
	
	@Resource(name="goodsServiceImpl")
	private GoodsService goodsServiceImpl;
	
	@Resource(name="articleServiceImpl")
	private ArticleService articleServiceImpl;
	
	@Resource(name="shoppingCartMiddleServiceImpl")
	private ShoppingCartMiddleService shoppingCartMiddleServiceImpl;
	
	@Resource(name="shoppingCartServiceImpl")
	private ShoppingCartService shoppingCartServiceImpl;
	
	private MD5 md5 = new MD5();

	@RequestMapping(value = "/info", method = RequestMethod.GET)  
    public String info(HttpServletRequest request,HttpServletResponse reponse) {  
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		reponse.setContentType("text/html;charset=UTF-8");
		
		Member member = null;
		int loginUser = 0;
		if(request.getSession().getAttribute("userid")!=null){
		   loginUser = (Integer) request.getSession().getAttribute("userid");
		}
		if(loginUser!=0){
			member = (Member) memberServiceImpl.findById(Member.class, loginUser);
		}
		
		if(member!=null){
			request.setAttribute("member", member);
			return "/user/info";
		}else{
			return "/login";
		}
    } 
	
	@RequestMapping(value = "/toupdatepwd", method = RequestMethod.GET)  
    public String toupdatepwd(HttpServletRequest request,HttpServletResponse reponse) {  
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		reponse.setContentType("text/html;charset=UTF-8");
		
		Member member = null;
		int loginUser = 0;
		if(request.getSession().getAttribute("userid")!=null){
		   loginUser = (Integer) request.getSession().getAttribute("userid");
		}
		if(loginUser!=0){
			member = (Member) memberServiceImpl.findById(Member.class, loginUser);
		}
		
		if(member!=null){
			request.setAttribute("member", member);
			return "/user/updatepwd";
		}else{
			return "/login";
		}
    } 
	
	@RequestMapping(value = "/checkpassword", method = RequestMethod.POST)  
    public void checkpassword(HttpServletRequest request,HttpServletResponse reponse) {  
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		reponse.setContentType("text/html;charset=UTF-8");
		
		String result = "error";
		String oldpwd = request.getParameter("oldpwd");
		Member member = null;
		int loginUser = 0;
		if(request.getSession().getAttribute("userid")!=null){
		   loginUser = (Integer) request.getSession().getAttribute("userid");
		}
		if(loginUser!=0){
			member = (Member) memberServiceImpl.findById(Member.class, loginUser);
		}
		
		if(member!=null){
			String md5Pwd = oldpwd;
			if(oldpwd!=null && !"".equals(oldpwd.trim())){
				md5Pwd = md5.md5s(oldpwd);
				if(md5Pwd.equals(member.getPassword())){
					result = "success";
				}
			}
		}
		
		try {
			PrintWriter writer = reponse.getWriter();
			writer.write(result);
			writer.flush();
			writer.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
    } 
	
	@RequestMapping(value = "/updatepwd", method = RequestMethod.POST)  
    public String updatepwd(HttpServletRequest request,HttpServletResponse reponse) {  
		try {
			request.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		reponse.setContentType("text/html;charset=UTF-8");
		
		String result = "error";
		String newpwd = request.getParameter("newpwd");
		Member member = null;
		int loginUser = 0;
		if(request.getSession().getAttribute("userid")!=null){
		   loginUser = (Integer) request.getSession().getAttribute("userid");
		}
		if(loginUser!=0){
			member = (Member) memberServiceImpl.findById(Member.class, loginUser);
		}
		
		if(member!=null){
			String md5Pwd = newpwd;
			if(newpwd!=null && !"".equals(newpwd.trim())){
				md5Pwd = md5.md5s(newpwd);
				member.setPassword(md5Pwd);
			}
			memberServiceImpl.save(member);
			result = "success";
			
			request.setAttribute("status", result);
			
			return "/user/updatepwd";
		}else{
			return "/login";
		}
    } 
}
